﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Web.Script.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ReadFile.WebService
{
    public enum SerializationType
    {
        /// <summary>
        /// XML格式
        /// </summary>
        XML,
        /// <summary>
        /// JSON格式
        /// </summary>
        Json,
        /// <summary>
        /// 字节流
        /// </summary>
        Byte,
    }
    /// <summary>
    /// 标准序列化扩展类
    /// </summary>
    public static class Serialization_Extension
    {
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="stringDataType"></param>
        /// <returns></returns>
        public static string Serialization(this object obj, SerializationType stringDataType)
        {
            string result = string.Empty;

            switch (stringDataType)
            {
                case SerializationType.Json:
                    result = ObjectToJson(obj);
                    break;
                case SerializationType.XML:
                    result = ObjectToXml(obj);
                    break;
            }

            return result;
        }

        public static byte[] Serialization(this object obj)
        {
            return ObjectToByteArray(obj);
        }
        /// <summary>
        /// 返序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="stringObject"></param>
        /// <param name="stringDataType"></param>
        /// <returns></returns>
        public static T Deserialization<T>(this string stringObject, SerializationType stringDataType) where T : class, new()
        {
            T obj = null;
            switch (stringDataType)
            {
                case SerializationType.Json:
                    obj = JsonToObject<T>(stringObject);
                    break;
                case SerializationType.XML:
                    obj = XmlToObject<T>(stringObject);
                    break;
            }
            return obj;
        }
        public static T Deserialization<T>(this byte[] obj) where T : class, new()
        {
            return ByteArrayToObject<T>(obj);
        }
        #region XML序列化与反序列化
        /// <summary>
        /// 将对像序列化为XML字符串
        /// </summary>
        /// <param name="obj">对像</param>
        /// <returns>Xml String</returns>
        private static string ObjectToXml(object obj)
        {
            XmlSerializer serializer = new XmlSerializer(obj.GetType());

            using (MemoryStream ms = new MemoryStream())
            {
                serializer.Serialize(ms, obj);

                StringBuilder result = new StringBuilder();

                result.Append(Encoding.UTF8.GetString(ms.ToArray()));

                return result.ToString();
            }
        }
        /// <summary>
        /// 将XML字符串返序列化为对像
        /// </summary>
        /// <typeparam name="ReturnType">对像类型</typeparam>
        /// <param name="xml">String</param>
        /// <returns>Object</returns>
        private static ReturnType XmlToObject<ReturnType>(string xml)
        {
            ReturnType rt;

            XmlSerializer serializer = new XmlSerializer(typeof(ReturnType));

            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            {
                rt = (ReturnType)serializer.Deserialize(ms);
            }

            return rt;
        }
        #endregion

        #region Json序列化与返序列化
        /// <summary>
        /// 将对像序列化JSON字符串
        /// </summary>
        /// <param name="obj">对像</param>
        /// <returns>Json String</returns>
        private static string ObjectToJson(object obj)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            return serializer.Serialize(obj);
        }

        /// <summary>
        /// 将JSON字符串返序列化对像
        /// </summary>
        /// <typeparam name="ReturnType">对像类型</typeparam>
        /// <param name="json">字符串</param>
        /// <returns>对像</returns>
        private static ReturnType JsonToObject<ReturnType>(string json)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            return serializer.Deserialize<ReturnType>(json);
        }
        #endregion

        #region Byte字节流序列化与返序列化
        private static byte[] ObjectToByteArray(object o)
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, o);
            return ms.ToArray();
        }

        private static ReturnType ByteArrayToObject<ReturnType>(byte[] ba)
        {
            MemoryStream ms = new MemoryStream(ba);
            BinaryFormatter bf = new BinaryFormatter();
            return (ReturnType)bf.Deserialize(ms);
        }
        #endregion
    }
}